<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Category extends Controller_DefaultTemplate{

public function action_index()
{
}

public function before()
{
	// Run anything that need ot run before this.
	parent::before();

	if (!Auth::instance()->logged_in())
	{
		$this->request->redirect('/user/login');
	}

}

public function action_list()
{
	$this->template->content = View::factory('book/category_list');
	$category = ORM::factory('category')->order_by('id', 'DESC')->find_all();
	$this->template->content->set('category_list', $category->as_array());
}

public function action_add()
{
	$this->template->content = View::factory('book/category_add');
}

public function action_new()
{
	if ($_POST)
    {
		/*
		$image_file_name = $this->save_image();
		if ($image_file_name == NULL)
		{
			echo "未上传图片";
			return;
		}
		 */

		// 保存category数据
		$category = ORM::factory('category');
		// $category->image = $image_file_name;
		$category->name = arr::get($_POST, 'name');
		try
		{
			$category->save();
		}
		catch (ORM_Validation_Exception $e)
        {
            $errors = $e->errors('model');
			print_r($errors);
			return;
        }

		$this->request->redirect('');
	}
}

public function action_delete()
{
	$id = $this->request->param('id');
	if ($id)
    {
		$category = ORM::factory('category')->where('id', '=', $id)->find();
		if ($category->loaded())
		{
			$category->delete();
		}
		$this->request->redirect('');
	}
}

public function action_modify()
{
	$id = $this->request->param('id');
	if ($id)
    {
		$category = ORM::factory('category')->where('id', '=', $id)->find();
		if ($category->loaded())
		{
			$this->template->content = View::factory('book/category_modify');
			$this->template->content->set('category', $category);
		}
	}
}


public function action_save()
{
	if ($_POST)
    {
		// 保存category数据
		$id = arr::get($_POST, 'id');
		$category = ORM::factory('category')->where('id', '=', $id)->find();;
		if ($category->loaded())
		{
			/*
			if (!empty($_FILES))
			{
				// 图片上传
				$tempFile = $_FILES['imgfile']['tmp_name'];
				if (file_exists($tempFile))
				{
					$targetPath = $_SERVER['DOCUMENT_ROOT'] . '/uploads' . '/';
					$fileParts = pathinfo($_FILES['imgfile']['name']);
					$image_file_name = md5_file($tempFile).".".$fileParts['extension'];
					$targetFile =  str_replace('//','/',$targetPath)."category/".$image_file_name;
					move_uploaded_file($tempFile, $targetFile);
					$category->image = $image_file_name;
				}
			}
			 */
		
			$category->name = arr::get($_POST, 'name');
			$category->update();
			echo "更新类别成功";
			return;
		}
	}
	echo "更新失败";
}

/*
protected function save_image()
{
	if ($_FILES["imgfile"]["error"] > 0)
	{
		return NULL;
	}

	// 图片上传
	try
	{ 
		$image = new Kohana_Image_GD($_FILES['imgfile']['tmp_name']);
		$fileParts = pathinfo($_FILES['imgfile']['name']);
		$image_file_name = md5_file($_FILES['imgfile']['tmp_name']).".".$fileParts['extension'];
		$targetFile =  str_replace('//','/',Kohana::$config->load('upload')->get('category')).$image_file_name;
		$image->save($targetFile);
		return $image_file_name;
	} 
	catch (Kohana_Exception $e)
	{
		echo 'Caught exception: ',  $e->getMessage(), "\n";
		return NULL;
	}
}
 */

}


